package com.class05.fruit.dao.impl;

import com.class05.fruit.dao.FruitDAO;
import com.class05.fruit.pojo.Fruit;
import com.class05.myssm.basedao.BaseDAO;

import java.util.List;

public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
    @Override
    public List<Fruit> getFruitList(String keyword,Integer pageNO) {
        return super.executeQuery("select * from t_fruit where fname like ? or remark like ? limit ?,5 ",
                "%"+keyword+"%","%"+keyword+"%",(pageNO - 1) * 5);
    }

    @Override
    public Fruit getFruitByFid(Integer fid) {
        String sql = "select * from t_fruit where fid=?";
        return super.load(sql, fid);
    }



    @Override
    public void updateFruit(Fruit fruit) {
        String sql = "update t_fruit set fname=? ,price=? " +
                "fcount =?,fruit remark=? where fid=?";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(),
                fruit.getFcount(), fruit.getRemark(), fruit.getFid());


    }

    @Override
    public void delFruit(Integer fid) {
        String sql = "delete from t_fruit where fid = ?";
        super.executeUpdate(sql, fid);

    }

    @Override
    public void addFruit(Fruit fruit) {
        String sql = "insert into t_fruit values(0,?,?,?,?)";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(),
                fruit.getFcount(), fruit.getRemark());
    }

    @Override
    public int getFruitCount(String keyword) {
        //方式一:
// return (int) super.executeComplexQuery("select count(*) from t_fruit")[0];
        //方式二:查询所有通过集合统计数量
        //List<Fruit> fruits = super.executeQuery("select * from t_fruit");
        //return fruits.size();
//方式三:
return ((Long) super.executeComplexQuery("select count(*) from t_fruit where fname like ? or remark like ?",
        "%"+keyword+"%","%"+keyword+"%")[0]).intValue();

    }


}

